package com.wkk.controller;

import com.wkk.entity.Coach;
import com.wkk.entity.Honor;
import com.wkk.entity.PerformanceInfo;
import com.wkk.entity.User;
import com.wkk.service.CoachService;
import com.wkk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.List;

@Controller
public class CoachController {

    @Autowired
    private CoachService coachService;

    @Autowired
    private UserService userService;


    //获取教练页面的数据信息
    @ResponseBody
    @RequestMapping("getPerformance")
    public PerformanceInfo getPerformanceInfo(@RequestParam("cid")Integer cid){
        PerformanceInfo performanceInfo = coachService.getCoachPerformanceInfo(cid);
        Calendar calendar = Calendar.getInstance();
        coachService.selectListOrderByRank("month",calendar.get(Calendar.MONTH)+1,0);
        performanceInfo.setCoachRank(coachService.selectOneCoach(cid).getRank());
        return performanceInfo;
    }

    /**
     * 实现会员请求教练连接
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("connectCoach")
    public User connectCoach(User user){
         coachService.userConnectCoach(user);
        return user;
    }
    /**
     * 教练确认会员请求
     * @param user
     */
    @ResponseBody
    @RequestMapping("connectCoachSuccess")
    public void connectCoachSuccess(User user){
        coachService.userConnectCoachSuccess(user);
    }


    /**
     * 教练取消会员联系
     */
    @ResponseBody
    @RequestMapping("connectCoachStop")
    public void  connectCoachStop(User user){
        coachService.userConnectCoachStop(user);
    }


    @ResponseBody
    @RequestMapping("connectMember")
    public String connectMember(@RequestParam("mobile")String mobile,@RequestParam("cid")Integer cid){
        //通过mobile查找到用户， 用户已有教练4 用户不存在返回3 用户不是大会员返回2
        User user =userService.selectOneByMobile(mobile);
        if(user == null){
            return "3";
        }else if (!user.getLevel().equals("member")){
            return "2";
        }else if(user.getCid()!=0){
            return "4";
        }
        //通过第一步，则将该用户的cid更新,并返回1
        user.setCid(cid);
        userService.updateUserCid(user);
        return "1";
    }

    @ResponseBody
    @RequestMapping("connectMemberSuccess")
    public void connectMemberSuccess(@RequestParam("uid")Integer uid){
        //如果用户确认连接,则改变cidState
        userService.updateUserCidState(uid);
    }

    @ResponseBody
    @RequestMapping("connectMemberStop")
    public void connectMemberStop(@RequestParam("uid")Integer uid){
        //教练提交该用户的uid 并将改变后的cid赋值为0 则实现教练取消连接
        userService.stopConnectByCoach(uid);
    }

    @ResponseBody
    @RequestMapping("getRequestByCoach")
    public Coach getRequestCoach(@RequestParam("uid")Integer uid){
        User user = userService.selectOne(uid);
        Coach coach = null;
        if(user.getCid()!=0&&user.getCidState()==0){
            coach = coachService.selectOneCoach(user.getCid());
            return coach;
        }else{
            coach = new Coach();
            coach.setCid(0);
            return coach;
        }
    }

    @ResponseBody
    @RequestMapping("getCoachInformation")
    public Coach getCoachInformation(HttpSession session){
        User nowUser =  (User) session.getAttribute("nowUser");
        Coach coach = new Coach();
        coach.setCid(0);
        if(nowUser.getCid()==0){
            return coach;
        }
        return coachService.selectOneCoach(nowUser.getCid());
    }
    /**
     * 按照教练业绩排名获取教练信息（业绩 = 公共课时*200+私教课时*对应不同级别教练价格+学员个数*100）
     * @return
     */
    //查询教练信排名(总排名、月度排名、年度排名)
    @ResponseBody
    @RequestMapping("getAllCoachInfo")
    public List<Coach> getAllCoachInfo(@RequestParam("condition")String condition,
                                       @RequestParam(value = "mouth",required = false,defaultValue = "0")Integer mouth,
                                       @RequestParam(value = "year",required = false,defaultValue = "0")Integer year){
        return coachService.selectListOrderByRank(condition,mouth,year);
    }

    //获取单个教练信息（包括业绩与学员信息）
    @ResponseBody
    @RequestMapping("getOneCoachInfo")
    public Coach getOneCoachInfo(@RequestParam("cid")Integer cid){
        return coachService.selectOneCoach(cid);
    }



    //添加教练，同时添加其登录信息
    @ResponseBody
    @RequestMapping("addCoach")
    public String addCoach(Coach coach){
        //在加入教练信息的同时，需要创建一个教练信息加入t_user表中,即教练可以通过用户的形式登录
        User user = coachService.addOrUpdateCoach(coach);
        return user.getUsername();
    }
    //删除教练
    @ResponseBody
    @RequestMapping("deleteCoach")
    public String deleteCoach(@RequestParam("cid")Integer cid){
        //删除教练的同时删除用户表中的教练信息
        if(coachService.deleteCoach(cid)){
            return "1";
        }else
            return "0";

    }

    //更新教练
    @ResponseBody
    @RequestMapping("updateCoachMold")
    public void updateCoach(Coach coach){
        coachService.updateCoachMold(coach);
    }

    //获取所有教练信息
    @ResponseBody
    @RequestMapping("getAllCoach")
    public List<Coach> getAllCoach(){
        //获取当月教练排行
        return coachService.selectListOrderByRank("month",0,0);
    }

    //获取某个教练的登录信息
    @ResponseBody
    @RequestMapping("getCoachLoginInfoByMobile")
    public User getCoachLoginInfoByMobile(@RequestParam("mobile")String mobile){
        return userService.selectOneByMobile(mobile);
    }

    //修改某个教练的指定荣誉
    @ResponseBody
    @RequestMapping("addOrUpdateHonor")
    public void addOrUpdateHonor(Honor honor){
        coachService.addOrUpdateHonor(honor);
    }

    //删除某个教练的指定荣誉
    @ResponseBody
    @RequestMapping("deleteHonor")
    public void deleteHonor(@RequestParam("id")Integer id){
        coachService.deleteHonor(id);
    }

    //添加荣誉到某个教练
    @ResponseBody
    @RequestMapping("getHonorsByCid")
    public List<Honor> addHonor(@RequestParam("cid")Integer cid){
       return coachService.selectListHonorByCid(cid);
    }

    //主管修改教练登录信息
    @ResponseBody
    @RequestMapping("changeLoginInfoPwd")
    public void changeLoginInfoPwd(User user){
        userService.userChangePassword(user);
    }


}
